from django import forms

from .models import PersonInfo, Vocation
from django.core.exceptions import ValidationError


def payment_validate(value):
    if value > str(30000):
        raise ValidationError('请输入合理的薪资')


class VocationForm(forms.Form):
    job = forms.CharField(max_length=20, label='职位')
    # 设置字段参数 widget、error_messages
    title = forms.CharField(max_length=20,
                            label='职称',
                            widget=forms.widgets.TextInput(attrs={'class': 'c1'}),
                            error_messages={'required': '职务不能为空'}
                            )
    # 设置字段参数 validators
    payment = forms.CharField(label='薪资', validators=[payment_validate], )

    # 设置下拉框的值
    # 查询模型PersonInfo的数据
    value = PersonInfo.objects.values('name')

    # 将数据以列表形式表示，列表元素为元组格式
    choices = [(i + 1, v['name']) for i, v in enumerate(value)]
    # 表单字段设置为choiceField类型，以生成下拉框
    person = forms.ChoiceField(choices=choices, label='姓名')

    # 自定义表单字段title的数据清洗
    def clean_title(self):
        data = self.cleaned_data['title']
        return '初级' + data
